<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否商家" prop="business">
        <el-input
          v-model="queryParams.business"
          placeholder="请输入是否商家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统登录id" prop="systemId">
        <el-input
          v-model="queryParams.systemId"
          placeholder="请输入登录id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['chat:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['chat:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['chat:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['chat:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="id" align="center" prop="id" />-->
<!--      <el-table-column label="uuid" align="center" prop="uuid" />-->
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="headImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.headImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="是否商家" align="center" >
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.chat_business_type" :value="scope.row.business"/>-->
<!--        </template>-->
        <template v-slot="slot">
          {{slot.row.business==1?'是':'否'}}
        </template>
      </el-table-column>
<!--      <el-table-column label="系统登录id" align="center" prop="systemId">-->

<!--        &lt;!&ndash; 级联系统登录的用户表，获取绑定的用户，并展示头像，最好是点击后能进入系统个人中心；需要注意的是，系统用户在没有聊天账号时要自动创建并关联一个&ndash;&gt;-->
<!--      </el-table-column>-->


      <el-table-column label="好友列表" align="center">
        <template v-slot="slot">

        <!-- 级联好友列表，可通过好友关联查询，通过弹窗或抽屉的方式在右边弹出展示-->
        <el-button @click="select_friend(slot.row.id)" type="primary" style="margin-left: 16px;">
          点击查看好友列表
        </el-button>
        </template>
      </el-table-column>





      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['chat:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['chat:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


<!--  用户好友信息  -->
    <el-drawer
      title="好友列表"
      :visible.sync="drawer"
      :before-close="handleClose"
      size="50%">

        <el-table
          :data="userLists"
          style="width: 100%">
<!--          <el-table-column-->
<!--            prop="id"-->
<!--            label="编号"-->
<!--            width="180">-->
<!--          </el-table-column>-->
          <el-table-column
            prop="nickName"
            label="姓名"
            width="180">
          </el-table-column>

          <el-table-column
            label="编辑"
            width="180">
            <template slot-scope="scope">
            <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  @click="friendDelete(scope.row)"
                  v-hasPermi="['chat:user:delete']"
                >删除</el-button>

            </el-col>
            </template>
          </el-table-column>
          <el-table-column
            label="个人信息"
            width="180">

            <el-button @click="innerDrawer = true">查看好友个人信息</el-button>
          </el-table-column>
        </el-table>



        <el-drawer
          title="个人信息"
          :append-to-body="true"
          :visible.sync="innerDrawer">
          <p>_(:зゝ∠)_</p>
        </el-drawer>

    </el-drawer>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改聊天用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="是否商家" prop="business">
          <el-input v-model="form.business" placeholder="请输入是否商家" />
        </el-form-item>
        <el-form-item label="头像" prop="headImage">
          <image-upload v-model="form.headImage"/>
        </el-form-item>
        <el-form-item label="系统登录id" prop="systemId">
          <el-input v-model="form.systemId" placeholder="请输入系统登录id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, getAllUser, deleteUser } from "@/api/chat/user";
import info from "../../logistics/info";


export default {
  name: "User",
  data() {
    return {
      //删除好友时的id
      modifyId:null,
      //用户好友信息
      userLists:[
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聊天用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        business: null,
        userId: null,
      },

      //头像
      images:"E:\\worksoft\\mall-cloud\\ruoyi-ui\\src\\views\\chat\\image\\202071523124040377.jpg",

      //用户好友
      drawer: false,
      innerDrawer: false,

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查询好友
    select_friend(id){
      this.modifyId = id
      this.drawer = true
      console.info(id)
      getAllUser(id,1).then(response => {
        console.info(response)
        this.userLists = response.data;
        console.info(this.userLists)
      })
    },

    /** 查询聊天用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        business: null,
        headImage: null,
        systemId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加聊天用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改聊天用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除好友操作 */
    friendDelete(row) {
      const ids = row.id || this.ids;
      console.info("md"+this.modifyId)
      this.$modal.confirm('是否确认删除该好友？').then(function() {

        return deleteUser(ids,this.modifyId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },




    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除聊天用户编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('chat/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },


    handleClose(done) {
          done();
    }
  }
};
</script>
